import { delField, getField, listField } from "@/api/service/field/field";
import { form, resetForm, showForm } from "./form";
import { keys, query, table } from "./table";

export const list = async () => {
  table.value.loading = true;

  const data = await listField(query.value);
  table.value.data = data.rows;
  query.value.total = data.total;
  table.value.loading = false;
};

export const edit = async (id?: string) => {
  const targetId = id || keys.value[0];
  const data = await getField(targetId);
  form.value = data.data;
  showForm.value = true;
};

export const del = async (id?: string) => {
  try {
    let ids = [];
    if (id) {
      ids = [id];
    } else {
      ids = keys.value;
    }
    table.value.loading = true;
    await delField(ids);
    await list();
    table.value.loading = false;
    keys.value = [];
  } catch (error) {
    table.value.loading = false;
  }
};
export const create = () => {
  resetForm();
  showForm.value = true;
};
